using System;
using System.Collections.Generic;

namespace CL
{
    public class CLTypeSystems
    {
        public class CLOneTypeSystems
        {
            public CLOneTypeSystems(int count)
            {
                this.QueueFlag = new bool[count];
            }

            public readonly Dictionary<Type, List<CLSystemObject>> Map = new();
            // 这里不用hash，数量比较少，直接for循环速度更快
            public readonly bool[] QueueFlag;
        }

        private readonly int count;

        public CLTypeSystems(int count)
        {
            this.count = count;
        }

        private readonly Dictionary<Type, CLOneTypeSystems> typeSystemsMap = new();

        public CLOneTypeSystems GetOrCreateOneTypeSystems(Type type)
        {
            CLOneTypeSystems systems = null;
            this.typeSystemsMap.TryGetValue(type, out systems);
            if (systems != null)
            {
                return systems;
            }

            systems = new CLOneTypeSystems(this.count);
            this.typeSystemsMap.Add(type, systems);
            return systems;
        }

        public CLOneTypeSystems GetOneTypeSystems(Type type)
        {
            CLOneTypeSystems systems = null;
            this.typeSystemsMap.TryGetValue(type, out systems);
            return systems;
        }

        public List<CLSystemObject> GetSystems(Type type, Type systemType)
        {
            CLOneTypeSystems oneTypeSystems = null;
            if (!this.typeSystemsMap.TryGetValue(type, out oneTypeSystems))
            {
                return null;
            }

            if (!oneTypeSystems.Map.TryGetValue(systemType, out List<CLSystemObject> systems))
            {
                return null;
            }

            return systems;
        }
    }
}
